# clean environment
rm(list=ls())

# load packages
library(ggplot2)
library(readstata13)

# load data set with parliamentary inquiries
dat <- read.csv("inquiries.csv")

# recode extremism variable
dat$extremism_reg <- NA
dat$extremism_reg[dat$extremism_v=="left"] <- 0
dat$extremism_reg[dat$extremism_v=="both"] <- 1
dat$extremism_reg[dat$extremism_v=="right"] <- 2
dat$extremism_reg <- as.numeric(as.character(dat$extremism_reg))

# english names of parties
dat$party_eng <- NA
dat$party_eng[dat$party=="AFD"] <- "AfD"
dat$party_eng[dat$party=="CDU"] <- "CDU/CSU"
dat$party_eng[dat$party=="FDP"] <- "FDP"
dat$party_eng[dat$party=="GRUENE"] <- "Greens"
dat$party_eng[dat$party=="LINKE"] <- "Left"
dat$party_eng[dat$party=="SPD"] <- "SPD"
dat$party_eng <- factor(dat$party_eng, levels=c("CDU/CSU", "AfD", "FDP", "Greens", "Left", "SPD"))

# create decade variable
dat$decade <- NA
dat$decade[dat$year<1960]<-1950
dat$decade[dat$year>=1960 & dat$year<1970]<-1960
dat$decade[dat$year>=1970 & dat$year<1980]<-1970
dat$decade[dat$year>=1980 & dat$year<1990]<-1980
dat$decade[dat$year>=1990 & dat$year<2000]<-1990
dat$decade[dat$year>=2000 & dat$year<2010]<-2000
dat$decade[dat$year>=2010 & dat$year<2020]<-2010

# create jurisdiction variable for merging
dat$jurisdiction2 <- dat$jurisdiction
dat$jurisdiction <- NA
dat$jurisdiction[dat$jurisdiction2=="bb"] <- "Brandenburg"
dat$jurisdiction[dat$jurisdiction2=="be"] <- "Berlin"
dat$jurisdiction[dat$jurisdiction2=="bt"] <- "Bund"
dat$jurisdiction[dat$jurisdiction2=="bw"] <- "Baden-Wurttemberg"
dat$jurisdiction[dat$jurisdiction2=="by"] <- "Bayern"
dat$jurisdiction[dat$jurisdiction2=="hb"] <- "Bremen"
dat$jurisdiction[dat$jurisdiction2=="he"] <- "Hessen"
dat$jurisdiction[dat$jurisdiction2=="hh"] <- "Hamburg"
dat$jurisdiction[dat$jurisdiction2=="mv"] <- "Mecklenburg-Vorpommern"
dat$jurisdiction[dat$jurisdiction2=="ni"] <- "Niedersachsen"
dat$jurisdiction[dat$jurisdiction2=="nw"] <- "Nordrhein-Westfalen"
dat$jurisdiction[dat$jurisdiction2=="rp"] <- "Rheinland-Pfalz"
dat$jurisdiction[dat$jurisdiction2=="sh"] <- "Schleswig-Holstein"
dat$jurisdiction[dat$jurisdiction2=="sl"] <- "Saarland"
dat$jurisdiction[dat$jurisdiction2=="sn"] <- "Sachsen"
dat$jurisdiction[dat$jurisdiction2=="st"] <- "Sachsen-Anhalt"
dat$jurisdiction[dat$jurisdiction2=="th"] <- "Thuringen"

# merge with far-right election results data
polbar <- read.dta13("polbar_agg.dta")
dat <- merge(dat, polbar, by=c("jurisdiction", "year"), all.x = T)

# code election year (i.e., those with Non-NA values in election result of far-right parties)
dat$elec_year = NA
dat$elec_year[!is.na(dat$elec_fr_vote_pct)] = 1
dat$elec_year[is.na(dat$elec_fr_vote_pct)] = 0

# regressions
mod_1<-lm(extremism_reg~factor(party_eng)*factor(elec_year), data=dat)
mod_2<-lm(extremism_reg~factor(party_eng)*factor(elec_year) + factor(jurisdiction), data=dat)
mod_3<-lm(extremism_reg~factor(party_eng)*factor(elec_year) + factor(jurisdiction) + factor(decade), data=dat)
mod_4<-lm(extremism_reg~factor(party_eng)*factor(elec_year) + factor(jurisdiction) + factor(year), data=dat)
